//
//  WDThinkingManager.m
//  WDPolymerizeSDK
//
//  Created by little People on 2023/6/13.
//

#import "WDThinkingManager.h"
#import "WDJsonManager.h"
#import "WDNSLog.h"

@import ThinkingSDK;
@implementation WDThinkingManager
{
    ThinkingAnalyticsSDK *instance;
}
static WDThinkingManager *_ThinkingManager = nil;
+ (instancetype)shareThinkingManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _ThinkingManager = [[self alloc] init];
    });
    return _ThinkingManager;
}

- (void)thinkingConfig
{
    
    TDConfig *config = [[TDConfig alloc] init];
    config.appid = [WDJsonManager thAppidForSDKConfigFile];
    config.serverUrl = [WDJsonManager thServerUrlForSDKConfigFile];
    if([WDNSLog logEnable]){
        config.mode = TDModeDebug;
    }else{
        config.mode = TDModeNormal;
    }
    config.defaultTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT+0800"];
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970]*1000;
    [TDAnalytics calibrateTime: time];
    [TDAnalytics startAnalyticsWithConfig:config];

    
    if([WDNSLog logEnable]){
        [TDAnalytics enableLog:TDLoggingLevelDebug];
    }else{
        [TDAnalytics enableLog:TDLoggingLevelNone];
    }
    
    [TDAnalytics enableThirdPartySharing:TDThirdPartyTypeAdjust | TDThirdPartyTypeAppLovin];

    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];//获取app版本信息

    NSString *short_build = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSString *app_BundleId = [infoDictionary objectForKey:@"CFBundleIdentifier"];

 
    
    [TDAnalytics enableAutoTrack:
    TDAutoTrackEventTypeAppStart |//APP 启动事件，记录 APP 启动或从后台恢复
    TDAutoTrackEventTypeAppInstall |//APP 安装，记录 APP 被安装的日志
    TDAutoTrackEventTypeAppEnd |//APP 关闭事件，记录 APP 调入后台
    TDAutoTrackEventTypeAppClick |//APP 点击控件事件
    TDAutoTrackEventTypeAppViewCrash];//APP 崩溃事件
    
    NSLog(@"--------%@------%@",short_build,app_BundleId);

    [TDAnalytics setSuperProperties:@{
                                                                @"version":short_build,
                                                                @"ditch_name":app_BundleId
                                                            }];
}

- (void)thinkingSetSuperPropertiesJsonStr:(id)jsonStr{

    NSDictionary *dict;
    if ([jsonStr isKindOfClass:[NSDictionary class]])
    {
        dict = (NSDictionary *)jsonStr;
    }
    else if ([jsonStr isKindOfClass:[NSString class]]){
        NSDictionary *temp = [WDJsonManager dictionaryWithJsonString:(NSString *)jsonStr];
        if (temp)
        {
            dict = temp;
        }
    }
    WDLog(@"公共属性--%@",dict);
    if (dict) {
        [TDAnalytics setSuperProperties:dict];
    }
}

- (void)thinkingTrackEventWithKey:(NSString *)key jsonStr:(id)jsonStr
{
    NSDictionary *dict;
    if ([jsonStr isKindOfClass:[NSDictionary class]])
    {
        dict = (NSDictionary *)jsonStr;
    }
    else if ([jsonStr isKindOfClass:[NSString class]]){
        NSDictionary *temp = [WDJsonManager dictionaryWithJsonString:(NSString *)jsonStr];
        if (temp)
        {
            dict = temp;
        }
    }
    NSLog(@"thinkingTrackEventWithKey:%@---%@",key,dict);
//    if (!instance) {
//        [self thinkingConfig];
//    }
    [TDAnalytics track:key properties:dict];
}


- (void)thinkingTrackLevelEnter:(NSInteger)level
{
    
    NSDictionary *dict = @{@"level":[NSString stringWithFormat:@"%ld",level+100000]};
    [self thinkingTrackEventWithKey:@"w_level_enter" jsonStr:dict];
}

- (void)thinkingTrackLevelStart:(NSInteger)level
{
    NSDictionary *dict = @{@"level":[NSString stringWithFormat:@"%ld",level+100000]};
    [self thinkingTrackEventWithKey:@"w_level_start" jsonStr:dict];
}

- (void)thinkingTrackLevelWin:(NSInteger)level
{
    NSDictionary *dict = @{@"level":[NSString stringWithFormat:@"%ld",level+100000]};
    [self thinkingTrackEventWithKey:@"w_level_win" jsonStr:dict];
}

- (void)thinkingTrackLevelFail:(NSInteger)level
{
    NSDictionary *dict = @{@"level":[NSString stringWithFormat:@"%ld",level+100000]};
    [self thinkingTrackEventWithKey:@"w_level_fail" jsonStr:dict];
}

- (void)thinkingTrackLevelRevive:(NSInteger)level
{
    NSDictionary *dict = @{@"level":[NSString stringWithFormat:@"%ld",level+100000]};
    [self thinkingTrackEventWithKey:@"w_level_revive" jsonStr:dict];
}

- (void)thinkingTrackLevelRetry:(NSInteger)level
{
    NSDictionary *dict = @{@"level":[NSString stringWithFormat:@"%ld",level+100000]};
    [self thinkingTrackEventWithKey:@"w_level_retry" jsonStr:dict];
}

- (void)thinkingTrackLevelProp:(NSInteger)level
{
    NSDictionary *dict = @{@"level":[NSString stringWithFormat:@"%ld",level+100000]};
    [self thinkingTrackEventWithKey:@"w_level_prop" jsonStr:dict];
}

- (void)thinkingTrackUserProperty:(id)json
{
    NSDictionary *dict;
    if ([json isKindOfClass:[NSDictionary class]])
    {
        dict = (NSDictionary *)json;
    }
    else if ([json isKindOfClass:[NSString class]]){
        NSDictionary *temp = [WDJsonManager dictionaryWithJsonString:(NSString *)json];
        if (temp)
        {
            dict = temp;
        }
    }
//    if (!instance) {
//        [self thinkingConfig];
//    }
    [TDAnalytics userSet:dict];
}

- (void)thinkingUser_setOnceWithKey:(NSString *)key value:(NSString *)value
{
//    if (!instance) {
//        [self thinkingConfig];
//    }
    [TDAnalytics userSetOnce:@{key:value}];
}

@end
